[Unit]
Description=Runners to orchestrate a high-availability PostgreSQL - Patroni
After=syslog.target network.target

[Service]
Type=simple

User=postgres
Group=postgres

# Read in configuration file if it exists, otherwise proceed
EnvironmentFile=-/etc/patroni_env.conf

# The default is the user's home directory, and if you want to change it, you must provide an absolute path.
# WorkingDirectory=~

# Where to send early-startup messages from the server
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Pre-commands to start watchdog device
# Uses '+' prefix to run as root without sudo (avoids SELinux init_t -> sudo_exec_t denial)
{% if patroni_watchdog_mode in ['automatic', 'required']  %}
ExecStartPre=-+/sbin/modprobe softdog
ExecStartPre=-+/bin/chown postgres {{ patroni_watchdog_device }}
{% else %}
#ExecStartPre=-+/sbin/modprobe softdog
#ExecStartPre=-+/bin/chown postgres {{ patroni_watchdog_device }}
{% endif %}

# Start the patroni process
{% if patroni_installation_method == 'pip' %}
ExecStart=/usr/local/bin/patroni /etc/patroni/patroni.yml
{% else %}
ExecStart=/usr/bin/patroni /etc/patroni/patroni.yml
{% endif %}

# Send HUP to reload from patroni.yml
ExecReload=/bin/kill -s HUP $MAINPID

# Only kill the patroni process, not it's children, so it will gracefully stop postgres
KillMode=process

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=60

# Restart the service if it crashed
Restart=on-failure

LimitNOFILE={{ patroni_systemd_limit_nofile | default(65536) }}

[Install]
WantedBy=multi-user.target
